package design.pattern.prototype.deep;

import java.io.*;
import java.time.LocalDateTime;

// 实现抽象原型类：Cloneable 的 clone方法
// Serializable序列化支持，可进行深拷贝

/**
 * @description: 齐天大圣
 * @author: liuzhangcheng
 **/
public class QiTianDaSheng extends Monkey implements Cloneable, Serializable {

    private JinGuBang jinGuBang;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return this.deepClone();
    }

    /**
     * 深克隆
     * @return
     */
    public QiTianDaSheng deepClone() {
        try{
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);

            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);

            QiTianDaSheng copy = (QiTianDaSheng)ois.readObject();
            copy.setDate(LocalDateTime.now());

            return copy;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    // 浅克隆
    public QiTianDaSheng shallowClone() {
        QiTianDaSheng copy = new QiTianDaSheng();
        copy.jinGuBang = this.jinGuBang;
        copy.setWeight(this.getWeight());
        copy.setHeight(this.getHeight());

        copy.setDate(LocalDateTime.now());

        return copy;
    }

    public JinGuBang getJinGuBang() {
        return jinGuBang;
    }

    public void setJinGuBang(JinGuBang jinGuBang) {
        this.jinGuBang = jinGuBang;
    }
}
